package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallStorage;
import org.linlinjava.litemall.db.mapper.LitemallStorageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallStorageService {
    @Autowired
    private LitemallStorageDao storageMapper;

    public void deleteByKey(String key) {
    	LitemallStorage example = new LitemallStorage();
    	example.setDeleted(true);
    	LambdaQuery<LitemallStorage> query = storageMapper.createLambdaQuery();
    	query.andEq(LitemallStorage::getKey, key);
    	query.updateSelective(example);
		/*
		 * LitemallStorageExample example = new LitemallStorageExample();
		 * example.or().andKeyEqualTo(key);
		 * storageMapper.logicalDeleteByExample(example);
		 */
    }

    public void add(LitemallStorage storageInfo) {
        storageInfo.setAddTime(LocalDateTime.now());
        storageInfo.setUpdateTime(LocalDateTime.now());
        storageMapper.insertTemplate(storageInfo,true);
        //storageMapper.insertSelective(storageInfo);
    }

    public LitemallStorage findByKey(String key) {
    	LitemallStorage example = new LitemallStorage();
    	example.setKey(key);
    	example.setDeleted(false);
    	return storageMapper.templateOne(example);
		/*
		 * LitemallStorageExample example = new LitemallStorageExample();
		 * example.or().andKeyEqualTo(key).andDeletedEqualTo(false);
		 *///return storageMapper.selectOneByExample(example);
    }

    public int update(LitemallStorage storageInfo) {
        storageInfo.setUpdateTime(LocalDateTime.now());
        return storageMapper.updateTemplateById(storageInfo);
        //return storageMapper.updateByPrimaryKeySelective(storageInfo);
    }

    public LitemallStorage findById(Integer id) {
    	return storageMapper.single(id);
       // return storageMapper.selectByPrimaryKey(id);
    }

    public List<LitemallStorage> querySelective(String key, String name, Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallStorage> query = storageMapper.createLambdaQuery();
    	 if (!StringUtils.isEmpty(key)) {
    		 query.andEq(LitemallStorage::getKey, key);
         }
         if (!StringUtils.isEmpty(name)) {
        	 query.andLike(LitemallStorage::getName, "%" + name + "%");
             //criteria.andNameLike("%" + name + "%");
         }
         query.andEq(LitemallStorage::getDeleted, false);

         if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        	 query.orderBy(sort + " " + order);
         }
         
         query.limit(page, limit);
    	return query.select();
		/*
		 * LitemallStorageExample example = new LitemallStorageExample();
		 * LitemallStorageExample.Criteria criteria = example.createCriteria();
		 * 
		 * if (!StringUtils.isEmpty(key)) { criteria.andKeyEqualTo(key); } if
		 * (!StringUtils.isEmpty(name)) { criteria.andNameLike("%" + name + "%"); }
		 * criteria.andDeletedEqualTo(false);
		 * 
		 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
		 * example.setOrderByClause(sort + " " + order); }
		 * 
		 * PageHelper.startPage(page, limit); return
		 * storageMapper.selectByExample(example);
		 */
    }
}
